#ifndef SCREEN_H
#define SCREEN_H

#include <iostream>
#include <string>

using namespace std;

class Window_mgr
{
public:
    void clear();
};

class Screen
{
    friend void Window_mgr::clear();
public:  // 构造函数
    Screen() = default;
    Screen(unsigned ht, unsigned wd)
        : height(ht), width(wd), contents(ht * wd, ' ') { }
    Screen(unsigned ht, unsigned wd, char c)
        : height(ht), width(wd), contents(ht * wd, c) { }
public:
    Screen &move(unsigned , unsigned );
    Screen &set(char );
    Screen &set(unsigned , unsigned , char);
    Screen &display(std::ostream &os) {
        do_display(os); return *this;
    }
    const Screen &display(std::ostream &os) const {
        do_display(os); return *this;
    }
private:
    void do_display(std::ostream &os) const {
        os << contents;
    }
private:
    unsigned height = 0, width = 0;
    unsigned cursor = 0;
    std::string contents;
};

void Window_mgr::clear()
{
    Screen myScreen(10, 20, 'X');
    cout << "清理之前 myScreen的内容是: " << endl;
    cout << myScreen.contents << endl;
    myScreen.contents = " ";
    cout << "清理之后 myScreen的内容是： " << endl;
    cout << myScreen.contents << endl;
}

inline Screen& Screen::move(unsigned r, unsigned c)
{
   unsigned row = r * width;
   cursor = row + c;
   return *this;
}

inline Screen& Screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}

inline Screen& Screen::set(unsigned row, unsigned col, char c)
{
    contents[row * width + col] = c;
    return *this;
}

#endif // SCREEN_H
